/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package be.ugent.medirem.logic;

import be.ugent.medirem.persistence.entity.details.ConsultationDetails;
import be.ugent.medirem.persistence.entity.details.GpDetails;
import be.ugent.medirem.persistence.entity.details.LocationDetails;
import be.ugent.medirem.persistence.entity.details.PatientDetails;
import be.ugent.medirem.persistence.entity.values.ConsultationValues;
import be.ugent.medirem.persistence.entity.values.GpValues;
import be.ugent.medirem.persistence.entity.values.LocationValues;
import be.ugent.medirem.persistence.exception.DaoException;
import be.ugent.medirem.persistence.exception.EntityNotFoundException;
import be.ugent.medirem.persistence.facade.ConsultationDaoLocal;
import be.ugent.medirem.persistence.facade.GpDaoLocal;
import be.ugent.medirem.persistence.facade.LocationDaoLocal;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 *
 * @author Thomas Roelens
 */
@Stateless
public class GpBean implements GpRemote {
    @EJB
    private ConsultationDaoLocal consultationDaoBean;
    @EJB
    private LocationDaoLocal locationDaoBean;
    @EJB
    private GpDaoLocal gpDaoBean;


    public Long getGpId(String username) throws IllegalArgumentException {
        return gpDaoBean.getGpId(username);
    }

    public Long create(GpValues gp) throws IllegalArgumentException {
        return gpDaoBean.create(gp, gp.getPassword());
    }

    public GpDetails getDetails(Long id) throws EntityNotFoundException, IllegalArgumentException {
        return gpDaoBean.getDetails(id);
    }

    public void update(GpValues gp) throws EntityNotFoundException, IllegalArgumentException {
        gpDaoBean.update(gp);
    }

    public void updatePassword(Long id, String old, String password) throws EntityNotFoundException, IllegalArgumentException {
        gpDaoBean.updatePassword(id, old, password);
    }

    public LocationDetails getLocation(Long gpId) throws EntityNotFoundException, IllegalArgumentException {
        return gpDaoBean.getLocation(gpId);
    }

    public List<PatientDetails> getPatients(Long id) throws EntityNotFoundException, IllegalArgumentException {
        return gpDaoBean.getPatients(id);
    }

    public void addPatient(Long id, Long patientId) throws EntityNotFoundException, IllegalArgumentException {
        gpDaoBean.addPatient(id, patientId);
    }

    public void updateGpLocation(Long gpId, Long locationIdToUpdate, LocationValues locationDetails) throws EntityNotFoundException, IllegalArgumentException {
        Long locationId = null;
        // Create a new Location
        if (locationIdToUpdate == null) {
            try {
                locationId = locationDaoBean.create(locationDetails);
            } catch (DaoException ex) {
                Logger.getLogger(PatientBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            try {
                locationDaoBean.update(locationIdToUpdate, locationDetails);
                locationId = locationIdToUpdate;
            } catch (EntityNotFoundException ex) {
                Logger.getLogger(PatientBean.class.getName()).log(Level.SEVERE, null, ex);
            } catch (DaoException ex) {
                Logger.getLogger(PatientBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        gpDaoBean.update(gpId, locationId);
    }

    public ConsultationDetails getLastConsultation(Long gpId, Long patientId) {
        return consultationDaoBean.getPreviousConsultation(gpId, patientId);
    }

    public ConsultationDetails getNextConsultation(Long gpId, Long patientId) {
        return consultationDaoBean.getNextConsultation(gpId, patientId);
    }

    public List<ConsultationDetails> getConsultations(Long id) {
        return consultationDaoBean.getGpConsultations(id);
    }

    public boolean hasPatient(Long id, Long patient) {
        return gpDaoBean.hasPatient(id, patient);
    }

    public Long makeConsultation(ConsultationValues values) throws EntityNotFoundException, IllegalArgumentException {
        return consultationDaoBean.create(values);
    }

}
